package com.kingcore.pda.file.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.kingcore.pda.base.controller.PdaAction;

public class FileDealAction  extends PdaAction {

	@Override
	public ActionForward executeAction(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	/* ------------------------------- for dwr begin ---------------------------------- */
	/**
	 * 
	 * <p>删除表文件。 dwr调用</p>
	 * @author Zeven on Feb 4, 2010
	 * @param tblEn
	 * @return
	 */
	public Map deleteTableFile(String prj_code, String tblEn){
	
		log.debug(prj_code+" "+tblEn);
		Map data = new HashMap();
		

		String conf_prefix=prj_code;	//从客户端取编号
		log.debug("projectCode = " + conf_prefix );
		//System.out.println ("aa"+conf_prefix);
		if(conf_prefix==null 
				|| conf_prefix.trim().equals("null") 
				|| conf_prefix.trim().equals("")){
			data.put("flag", "fail");
			data.put("echo", "项目编号"+conf_prefix+"不存在!");
			return data;
		}
		
		conf_prefix=conf_prefix+".";
		//System.out.println ("current project code is "+conf_prefix);

		WebContext ctx = WebContextFactory.get();
		HttpServletRequest request = ctx.getHttpServletRequest();
		log.debug("conf_prefix = " + conf_prefix );
		java.util.Properties pro=new java.util.Properties();
		
		try {
			pro.load(new java.io.FileInputStream(request.getRealPath("")+"/WEB-INF/conf/pda-project.properties"));
			String rootPath=pro.getProperty( conf_prefix+"expPath");
			log.debug("rootPath = " + rootPath );

			//查找文件并删除
			tblEn = tblEn.toLowerCase();
			java.io.File file = new java.io.File( rootPath );
			String[] fileNames= file.list();
			File[] files= file.listFiles();
			for(int i=0;i<fileNames.length;i++){
				if(fileNames[i].toLowerCase().startsWith(tblEn)
						&& fileNames[i].endsWith(".woo.table")){
					log.info("删除文件："+fileNames[i]);
					files[i].delete();
					data.put("flag", "succeed");
					data.put("echo", "删除文件成功!");
					return data;
				}
			}
		} catch (Exception e) {
			log.error(e);
			data.put("flag", "fail");
			data.put("echo", "删除文件失败!\n"+e.getMessage());
			return data;
		}
		return null;
	}
	
	/* ------------------------------- for dwr end ---------------------------------- */

}
